﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class BUserSysRightM : INTPage
{
    SqlHelp help = new SqlHelp();
    CommonMethod comHelp = new CommonMethod();

    protected void Page_Load(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl2);
        if (!IsPostBack)
        { UserSysRight(); }
    }

    #region 初始化绑定 UserSysRight(string UserCode)
    protected void UserSysRight()
    {
        //权限
        comHelp.GetRightUnite(this, new string[] { Session["UserCode"].ToString(), Session["CoCode"].ToString(), "BUserSysRight" },
            new string[] { "Button1", "Button2", "Button3", "Button4" }, new int[] { 1, 3, 1, 3 }, CommonModel.loginOut2);
        //绑定用户组
        UserGroupBind();
        UserListBind(ddlUserGroupName.SelectedValue.ToString());
        //绑定页面组
        WebGroupBind();
        WebListBind(ddlWebGroupName.SelectedValue.ToString());
        //权限绑定
        WGUGRightBind(ddlWebGroupName.SelectedValue.ToString(), ddlUserGroupName.SelectedValue.ToString());
    }

    //绑定页面组
    protected void WebGroupBind()
    {
        string StrWebGroupSql = " select WG.WGCode,WG.WGName from B_SysWebGroup WG where WG.DeleteFlag<>1 ";
        ddlWebGroupName.DataTextField = "WGName";
        ddlWebGroupName.DataValueField = "WGCode";
        ddlWebGroupName.DataSource = help.GetDataTable(StrWebGroupSql);
        ddlWebGroupName.DataBind();
    }
    //组的页面
    protected void WebListBind(string WGCode)
    {   //清空
        string StrSql = "select S.WebType,S.WebName from B_SysWeb S join "
            + " B_SysWebAndGroup SWG on S.WebType=SWG.WebType where	SWG.WGCode='" + WGCode + "' ";
        lbxWebName.DataSource = help.GetDataTable(StrSql);
        lbxWebName.DataValueField = "WebType";
        lbxWebName.DataTextField = "WebName";
        lbxWebName.DataBind();
    }
    //绑定用户组
    protected void UserGroupBind()
    {
        string StrUserGroupeSql = " select UGCode,UGName from B_UserGroup where DeleteFlag<>1 ";
        ddlUserGroupName.DataTextField = "UGName";
        ddlUserGroupName.DataValueField = "UGCode";
        ddlUserGroupName.DataSource = help.GetDataTable(StrUserGroupeSql);
        ddlUserGroupName.DataBind();
    }
    //组的用户
    protected void UserListBind(string UGCode)
    {   //清空
        lbxUserName.Items.Clear();
        string StrSql = "select U.UserCode,U.UserEngName+'-'+U.UserFullName UserName from B_User U join B_UserAndGroup UG "
            + " on U.UserCode=UG.UserCode where UG.UGCode='" + UGCode + "' order by U.UserEngName ";
        lbxUserName.DataSource = help.GetDataTable(StrSql);
        lbxUserName.DataValueField = "UserCode";
        lbxUserName.DataTextField = "UserName";
        lbxUserName.DataBind();
    }
    #endregion

    //分别绑定未在此页面组和用户组的权限的列表
    protected void WGUGRightBind(string WGCode,string UGCode)
    {
        //未在组里的
        for (int i = 0; i < lbxInRightName.Items.Count; i++)
        { lbxNotInRightName.Items.Add(lbxInRightName.Items[i]); }
        //在组里的
        string StrWNGSql = " select RightType,RightName from B_UserSysRight where UGCode='"
            + UGCode + "' and  WGCode='" + WGCode + "'";
        DataTable Dt = help.GetDataTable(StrWNGSql);
        lbxInRightName.DataSource = Dt;
        lbxInRightName.DataValueField = "RightType";
        lbxInRightName.DataTextField = "RightName";
        lbxInRightName.DataBind();

        lbxInsert2.DataSource = Dt;
        lbxInsert2.DataValueField = "RightType";
        lbxInsert2.DataTextField = "RightName";
        lbxInsert2.DataBind();

        //未在组里的
        for (int i = 0; i < lbxInRightName.Items.Count; i++)
        { lbxNotInRightName.Items.Remove(lbxInRightName.Items[i]); }
    }

    //根据页面组动态绑定页面列表
    protected void ddlWebGroupName_TextChanged(object sender, EventArgs e)
    {
        LbxClear();
        WebListBind(ddlWebGroupName.SelectedValue.ToString());
        WGUGRightBind(ddlWebGroupName.SelectedValue.ToString(), 
            ddlUserGroupName.SelectedValue.ToString());
    }
    //根据用户组动态绑定用户列表
    protected void ddlUserGroupName_TextChanged(object sender, EventArgs e)
    {
        LbxClear();
        UserListBind(ddlUserGroupName.SelectedValue.ToString());
        WGUGRightBind(ddlWebGroupName.SelectedValue.ToString(), 
            ddlUserGroupName.SelectedValue.ToString());
    }

    #region 页面组与用户组权限分配
    //单项添加
    protected void Button1_Click(object sender, EventArgs e)
    {
        //不在组里的页面数
        for (int i = 0; i < lbxNotInRightName.Items.Count; i++)
        {
            if (lbxNotInRightName.SelectedIndex >= 0)
            {
                lbxInRightName.Items.Add(lbxNotInRightName.SelectedItem);

                //是否插入
                int INum = 0;
                string StrUserName = lbxNotInRightName.SelectedItem.Text.ToString();

                for (int d = 0; d < lbxInsert2.Items.Count; d++)
                {
                    if (StrUserName.Equals(lbxInsert2.Items[d].Text.ToString()))
                    { ++INum; }
                }

                if (INum < 1)
                { lbxInsert.Items.Add(lbxNotInRightName.SelectedItem); }

                lbxNotInRightName.Items.Remove(lbxNotInRightName.SelectedItem);
                lbxDelete.Items.Remove(lbxNotInRightName.SelectedItem);
            }
        }
    }
    //单项移除
    protected void Button2_Click(object sender, EventArgs e)
    {
        //不在组里的
        for (int i = 0; i < lbxInRightName.Items.Count; i++)
        {
            if (lbxInRightName.SelectedIndex >= 0)
            {
                lbxNotInRightName.Items.Add(lbxInRightName.SelectedItem);
                //插入行
                int INum = 0;
                //页面类型
                string WebTypeStr = lbxInRightName.SelectedItem.Text.ToString();

                for (int k = 0; k < lbxInsert2.Items.Count; k++)
                {
                    if (WebTypeStr.Equals(lbxInsert2.Items[k].Text.ToString()))
                    {
                        ++INum;
                    }
                }
                if (INum > 0)
                {
                    lbxDelete.Items.Add(lbxInRightName.SelectedItem);
                }
                lbxInsert.Items.Remove(lbxInRightName.SelectedItem);
                lbxInRightName.Items.Remove(lbxInRightName.SelectedItem);
            }
        }
    }
    //全部添加
    protected void Button4_Click(object sender, EventArgs e)
    {
        //在删除组的
        for (int j = 0; j < lbxDelete.Items.Count; j++)
        {
            lbxNotInRightName.Items.Remove(lbxDelete.Items[j]);
            lbxInRightName.Items.Add(lbxDelete.Items[j]);
        }
        //不在组里的
        for (int i = 0; i < lbxNotInRightName.Items.Count; i++)
        {
            lbxInRightName.Items.Add(lbxNotInRightName.Items[i]);
            lbxInsert.Items.Add(lbxNotInRightName.Items[i]);
        }
        lbxNotInRightName.Items.Clear();
        lbxDelete.Items.Clear();
    }
    //全部删除
    protected void Button3_Click(object sender, EventArgs e)
    {
        //在添加组的
        for (int k = 0; k < lbxInsert.Items.Count; k++)
        {
            lbxNotInRightName.Items.Add(lbxInsert.Items[k]);
            lbxInRightName.Items.Remove(lbxInsert.Items[k]);
        }
        //不在组里的
        for (int i = 0; i < lbxInRightName.Items.Count; i++)
        {
            lbxNotInRightName.Items.Add(lbxInRightName.Items[i]);
            lbxDelete.Items.Add(lbxInRightName.Items[i]);
        }
        lbxInRightName.Items.Clear();
        lbxInsert.Items.Clear();
    }
    #endregion

    //保存
    protected void btnSave_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl2);
        string Str = string.Empty;//操作字符串
        //页面组编号
        string WGCode = ddlWebGroupName.SelectedValue.ToString();
        //用户组编号
        string UGCode = ddlUserGroupName.SelectedValue.ToString();

        //当前用户
        string UserName = Session["UserCode"].ToString();
        //当前日期
        string StrDate = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm");

        //统计插入与删除的行数
        int InsertN = lbxInsert.Items.Count;
        int DeleteN = lbxDelete.Items.Count;

        //删除
        if (DeleteN > 0)
        {   //删除SQL语句前部分
            string DeleteStr = " Delete from B_UserSysRight where WGCode='"
                + WGCode + "' and UGCode='" + UGCode + "' and RightType='";
            for (int j = 0; j < DeleteN; j++)
            { Str += DeleteStr + lbxDelete.Items[j].Value.ToString() + "';   "; }
        }//插入
        if (InsertN > 0)
        {   //插入SQL语句前部分
            string InsertStr = " Insert Into B_UserSysRight(DeleteFlag,AddAuthor,"
                + "AddDate,UpdateAuthor,UpdateDate,WGCode,UGCode,RightType,RightName) values('0','" + UserName
                + "','" + StrDate + "','" + UserName + "','" + StrDate + "','" + WGCode + "','" + UGCode + "','";

            for (int i = 0; i < InsertN; i++)
            {
                Str += InsertStr + lbxInsert.Items[i].Value.ToString()
                    + "','" + lbxInsert.Items[i].Text.ToString() + "');  ";
            }
        }
        
        if (!string.IsNullOrEmpty(Str))
        {
            if (help.RunSqlInt(Str) > 0)
            {
                CommonMethod.regAlertMessage("操作成功！", this);
                WGUGRightBind(ddlWebGroupName.SelectedItem.Value.ToString(),
                    ddlUserGroupName.SelectedValue.ToString());
                LbxClear();
            }
            else { CommonMethod.regAlertMessage("操作失败！", this); }
        }
        else { CommonMethod.regAlertMessage("选择项没有变动！", this); }
    }
    //清空
    protected void LbxClear()
    {
        lbxDelete.Items.Clear();
        lbxInsert.Items.Clear();
        lbxInsert2.Items.Clear();
    }
}
